//
// Created by Kqj on 25-8-26.
//

#include "lcd.h"
#include <stdbool.h>
#include "stm32f1xx_hal.h"
#include "gpio.h"
#include "stdio.h"




#define TFT_COLUMN_NUMBER 240
#define TFT_LINE_NUMBER 320
#define TFT_COLUMN_OFFSET 0


const unsigned char  chines_word[   ][32]=   //汉字码
        {
/*
[总字符库]："手动模式恒压功率最小阈值大"*/

/*-- ID:0,字符:"手",ASCII编码:CAD6,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
                0x00,0x08,0x00,0x1f,0xfe,0x00,0x80,0x00,0x80,0x08,0xfc,0x1f,0x80,0x00,0x80,0x00,
                0x80,0x20,0xff,0x7f,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0xa0,0x00,0x40,0x00,

/*-- ID:1,字符:"动",ASCII编码:B6AF,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
                0x00,0x02,0x10,0x02,0x3e,0x02,0x00,0x22,0xa0,0x7f,0x7f,0x22,0x08,0x22,0x08,0x22,
                0x04,0x22,0x24,0x22,0x42,0x21,0x7f,0x21,0x82,0x20,0x80,0x20,0x40,0x14,0x20,0x08,

/*-- ID:2,字符:"模",ASCII编码:C4A3,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
                0x88,0x08,0x88,0x28,0xf8,0x7f,0x88,0x08,0xbf,0x18,0xc8,0x3f,0x4c,0x10,0xdc,0x1f,
                0x6a,0x10,0xca,0x1f,0x09,0x02,0xf8,0x7f,0x08,0x02,0x08,0x0d,0x88,0x70,0x68,0x20,

/*-- ID:3,字符:"式",ASCII编码:CABD,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
                0x00,0x01,0x00,0x05,0x00,0x09,0x00,0x21,0xff,0x7f,0x00,0x01,0x00,0x01,0x7c,0x01,
                0x10,0x01,0x10,0x02,0x10,0x02,0x90,0x04,0x70,0x44,0x0e,0x48,0x04,0x50,0x00,0x20,

/*-- ID:4,字符:"恒",ASCII编码:BAE3,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
                0x08,0x10,0xe8,0x3f,0x08,0x00,0x08,0x10,0xda,0x3f,0x6a,0x10,0x4a,0x10,0xc9,0x1f,
                0x48,0x10,0x48,0x10,0xc8,0x1f,0x08,0x00,0x08,0x20,0xf8,0x7f,0x08,0x00,0x08,0x00,

/*-- ID:5,字符:"压",ASCII编码:D1B9,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
                0x00,0x10,0xfc,0x3f,0x04,0x01,0x04,0x01,0x04,0x01,0x04,0x01,0x04,0x11,0xf4,0x3f,
                0x04,0x01,0x04,0x05,0x04,0x19,0x04,0x11,0x02,0x01,0x02,0x21,0xf9,0x7f,0x00,0x00,

/*-- ID:6,字符:"功",ASCII编码:B9A6,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
                0x00,0x01,0x00,0x01,0x10,0x01,0x3f,0x01,0x08,0x21,0xe8,0x7f,0x08,0x21,0x08,0x21,
                0x08,0x21,0x08,0x21,0xb8,0x20,0x8f,0x20,0x82,0x20,0x40,0x22,0x20,0x14,0x10,0x08,

/*-- ID:7,字符:"率",ASCII编码:C2CA,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
                0x40,0x00,0x80,0x10,0xfe,0x3f,0x80,0x00,0x42,0x22,0xe4,0x11,0x88,0x08,0x44,0x12,
                0xf2,0x27,0x80,0x04,0x80,0x20,0xff,0x7f,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,

/*-- ID:8,字符:"最",ASCII编码:D7EE,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
                0xf8,0x0f,0x08,0x08,0xf8,0x0f,0x08,0x08,0xf8,0x0f,0x00,0x20,0xff,0x7f,0x44,0x00,
                0xfc,0x3f,0x44,0x21,0x7c,0x11,0x44,0x0a,0x7c,0x04,0x47,0x1a,0x40,0x71,0xc0,0x20,

/*-- ID:9,字符:"小",ASCII编码:D0A1,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
                0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0xa0,0x02,0xa0,0x04,0x90,0x08,
                0x90,0x10,0x88,0x20,0x84,0x20,0x82,0x00,0x80,0x00,0x80,0x00,0xa0,0x00,0x40,0x00,

/*-- ID:10,字符:"阈",ASCII编码:E3D0,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
                0x04,0x20,0xe8,0x7f,0x02,0x25,0x02,0x29,0xfe,0x3f,0x02,0x21,0x7a,0x21,0x4a,0x29,
                0x4a,0x2a,0x4a,0x26,0x7a,0x22,0x02,0x2a,0x7e,0x2d,0x82,0x28,0x42,0x20,0x02,0x30,

/*-- ID:11,字符:"值",ASCII编码:D6B5,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
                0x10,0x02,0x10,0x12,0xf0,0x3f,0x08,0x02,0x08,0x12,0xcc,0x3f,0x4a,0x10,0xc9,0x1f,
                0x48,0x10,0xc8,0x1f,0x48,0x10,0xc8,0x1f,0x48,0x10,0x48,0x10,0xf8,0x7f,0x08,0x00,

/*-- ID:12,字符:"大",ASCII编码:B4F3,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
                0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x20,0xff,0x7f,0x80,0x00,0x40,0x01,
                0x40,0x01,0x40,0x02,0x20,0x02,0x20,0x04,0x10,0x08,0x08,0x70,0x06,0x20,0x00,0x00,

 /*-- ID:13,字符:"℃",ASCII编码:,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
                0x0c,0x00,0x12,0x00,0x12,0x00,0xcc,0x17,0x60,0x1c,0x30,0x18,0x30,0x10,0x30,0x00,
                0x30,0x00,0x30,0x00,0x30,0x00,0x60,0x10,0xc0,0x08,0x80,0x07,0x00,0x00,0x00,0x00




        };


const unsigned char * chines [5][4] = {
        chines_word[手],chines_word[动],chines_word[模],chines_word[式],
        chines_word[恒],chines_word[压],chines_word[模],chines_word[式],
        chines_word[恒],chines_word[压],chines_word[功],chines_word[率],
        chines_word[最],chines_word[小],chines_word[阈],chines_word[值],
        chines_word[最],chines_word[大],chines_word[阈],chines_word[值]
};


const unsigned char  english_char[   ][16]= //英文码
        {

                /* [字库]：[ASC8x16E字库] [数据排列]:从左到右从上到下 [取模方式]:横向8点右高位 [正负反色]:否 [去掉重复后]共11个字符
             [总字符库]："1234567890-"*/
/*-- ID:0,字符:"0",ASCII编码:30,对应字:宽x高=8x16,画布:宽W=8 高H=16,共16字节*/
                0x00,0x00,0x18,0x24,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x24,0x18,0x00,0x00,0x00,

/*-- ID:1,字符:"1",ASCII编码:31,对应字:宽x高=8x16,画布:宽W=8 高H=16,共16字节*/
                0x00,0x00,0x10,0x1c,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x7c,0x00,0x00,0x00,

/*-- ID:1,字符:"2",ASCII编码:32,对应字:宽x高=8x16,画布:宽W=8 高H=16,共16字节*/
                0x00,0x00,0x3c,0x42,0x42,0x42,0x40,0x20,0x10,0x08,0x04,0x42,0x7e,0x00,0x00,0x00,

/*-- ID:2,字符:"3",ASCII编码:33,对应字:宽x高=8x16,画布:宽W=8 高H=16,共16字节*/
                0x00,0x00,0x3c,0x42,0x42,0x40,0x20,0x18,0x20,0x40,0x42,0x42,0x3c,0x00,0x00,0x00,

/*-- ID:3,字符:"4",ASCII编码:34,对应字:宽x高=8x16,画布:宽W=8 高H=16,共16字节*/
                0x00,0x00,0x20,0x30,0x30,0x28,0x24,0x24,0x22,0xfe,0x20,0x20,0xf8,0x00,0x00,0x00,

/*-- ID:4,字符:"5",ASCII编码:35,对应字:宽x高=8x16,画布:宽W=8 高H=16,共16字节*/
                0x00,0x00,0x7e,0x02,0x02,0x02,0x1e,0x22,0x40,0x40,0x42,0x22,0x1c,0x00,0x00,0x00,

/*-- ID:5,字符:"6",ASCII编码:36,对应字:宽x高=8x16,画布:宽W=8 高H=16,共16字节*/
                0x00,0x00,0x18,0x24,0x02,0x02,0x3a,0x46,0x42,0x42,0x42,0x44,0x38,0x00,0x00,0x00,

/*-- ID:6,字符:"7",ASCII编码:37,对应字:宽x高=8x16,画布:宽W=8 高H=16,共16字节*/
                0x00,0x00,0x7e,0x42,0x20,0x20,0x10,0x10,0x08,0x08,0x08,0x08,0x08,0x00,0x00,0x00,

/*-- ID:7,字符:"8",ASCII编码:38,对应字:宽x高=8x16,画布:宽W=8 高H=16,共16字节*/
                0x00,0x00,0x3c,0x42,0x42,0x42,0x24,0x18,0x24,0x42,0x42,0x42,0x3c,0x00,0x00,0x00,

/*-- ID:8,字符:"9",ASCII编码:39,对应字:宽x高=8x16,画布:宽W=8 高H=16,共16字节*/
                0x00,0x00,0x1c,0x22,0x42,0x42,0x42,0x62,0x5c,0x40,0x40,0x24,0x18,0x00,0x00,0x00,


/*-- ID:10,字符:"-",ASCII编码:2D,对应字:宽x高=8x16,画布:宽W=8 高H=16,共16字节*/
                0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

                /* [字库]：[ASC8x16E字库] [数据排列]:从左到右从上到下 [取模方式]:横向8点右高位 [正负反色]:否 [去掉重复后]共3个字符
  [总字符库]："PSI"*/

/*-- ID:11,字符:"P",ASCII编码:50,对应字:宽x高=8x16,画布:宽W=8 高H=16,共16字节*/
                0x00,0x00,0x3f,0x42,0x42,0x42,0x42,0x3e,0x02,0x02,0x02,0x02,0x07,0x00,0x00,0x00,

/*-- ID:12,字符:"S",ASCII编码:53,对应字:宽x高=8x16,画布:宽W=8 高H=16,共16字节*/
                0x00,0x00,0x7c,0x42,0x42,0x02,0x04,0x18,0x20,0x40,0x42,0x42,0x3e,0x00,0x00,0x00,

/*-- ID:13,字符:"I",ASCII编码:49,对应字:宽x高=8x16,画布:宽W=8 高H=16,共16字节*/
                0x00,0x00,0x3e,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x3e,0x00,0x00,0x00,

/*-- ID:14,字符:"V",ASCII编码:56,对应字:宽x高=8x16,画布:宽W=8 高H=16,共16字节*/
                0x00,0x00,0xe7,0x42,0x42,0x22,0x24,0x24,0x14,0x14,0x18,0x08,0x08,0x00,0x00,0x00,
/*-- ID:15,字符:"空白",ASCII编码:56,对应字:宽x高=8x16,画布:宽W=8 高H=16,共16字节*/

                0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                /* [字库]：[ASC8x16E字库] [数据排列]:从左到右从上到下 [取模方式]:横向8点右高位 [正负反色]:否 [去掉重复后]共1个字符
 [总字符库]："%"*/

/*-- ID:16,字符:"%",ASCII编码:25,对应字:宽x高=8x16,画布:宽W=8 高H=16,共16字节*/
                0x00,0x00,0x22,0x25,0x15,0x15,0x0d,0x2a,0x58,0x54,0x54,0x52,0x22,0x00,0x00,0x00,

        };


void  set_data_command(bool ctrl)
{
    if(ctrl == 1)
        HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,1);
    else
        HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,0);

}

void  reset(bool ctrl)
{
    if(ctrl == 1)
        HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,1);
    else
        HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,0);

}

void  lcd_cs(bool ctrl)
{
    if(ctrl == 1)
        HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,1);
    else
        HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,0);

}
/*写指令到LCD模块*/
void transfer_command(uint8_t data1)
{
    set_data_command(0);
    HAL_SPI_Transmit(&hspi1,&data1,sizeof(data1),999);
}


/*写数据到LCD模块*/
void transfer_data(uint8_t data1)
{
    //cs1=0;
    set_data_command(1);
    HAL_SPI_Transmit(&hspi1,&data1,sizeof(data1),999);
}

void transfer_data_fast(uint8_t data1)
{
    //cs1=0;
//    set_data_command(1);
    HAL_SPI_Transmit(&hspi1,&data1,sizeof(data1),999);
}

//===传16位指令，16位指令一起赋值
void transfer_command_16(uint com_16bit)
{
    transfer_command(com_16bit >>8); 	//先传高8位
    transfer_command(com_16bit );	  	//再传低8位
}

//连写2个字节（即16位）数据到LCD模块
void transfer_data_16(uint data_16bit)
{
    transfer_data(data_16bit>>8);
    transfer_data(data_16bit);
}

//===发送1个字节的指令及1个字节的数据================
void Lcd_Write_Com_Data(uint com,uint val)
{
    transfer_command_16(com);	//先传指令
    transfer_data_16(val);  	//再传数据
}

void lcd_initial()
{
    reset(0);
    HAL_Delay(200);
    reset(1);
    HAL_Delay(200);
//************* Start Initial Sequence **********//
    transfer_command(0x01);//Software Reset
    HAL_Delay(120);

    transfer_command(0x11); 			//Sleep Out
    HAL_Delay(120);               //DELAY120ms
    //-----------------------ST7789V Frame rate setting-----------------//
//************************************************
    transfer_command(0x3A);        //65k mode
    transfer_data(0x05);
    transfer_command(0xC5); 		//VCOM
    transfer_data(0x1A);
    transfer_command(0x36);                 // 屏幕显示方向设置
    transfer_data(0x60);  //00,60,A0
    //-------------ST7789V Frame rate setting-----------//
    transfer_command(0xb2);		//Porch Setting
    transfer_data(0x05);
    transfer_data(0x05);
    transfer_data(0x00);
    transfer_data(0x33);
    transfer_data(0x33);

    transfer_command(0xb7);			//Gate Control
    transfer_data(0x05);			//12.2v   -10.43v
    //--------------ST7789V Power setting---------------//
    transfer_command(0xBB);//VCOM
    transfer_data(0x3F);

    transfer_command(0xC0); //Power control
    transfer_data(0x2c);

    transfer_command(0xC2);		//VDV and VRH Command Enable
    transfer_data(0x01);

    transfer_command(0xC3);			//VRH Set
    transfer_data(0x0F);		//4.3+( vcom+vcom offset+vdv)

    transfer_command(0xC4);			//VDV Set
    transfer_data(0x20);				//0v

    transfer_command(0xC6);				//Frame Rate Control in Normal Mode
    transfer_data(0X01);			//111Hz

    transfer_command(0xd0);				//Power Control 1
    transfer_data(0xa4);
    transfer_data(0xa1);

    transfer_command(0xE8);				//Power Control 1
    transfer_data(0x03);

    transfer_command(0xE9);				//Equalize time control
    transfer_data(0x09);
    transfer_data(0x09);
    transfer_data(0x08);
    //---------------ST7789V gamma setting-------------//
    transfer_command(0xE0); //Set Gamma
    transfer_data(0xD0);
    transfer_data(0x05);
    transfer_data(0x09);
    transfer_data(0x09);
    transfer_data(0x08);
    transfer_data(0x14);
    transfer_data(0x28);
    transfer_data(0x33);
    transfer_data(0x3F);
    transfer_data(0x07);
    transfer_data(0x13);
    transfer_data(0x14);
    transfer_data(0x28);
    transfer_data(0x30);

    transfer_command(0XE1); //Set Gamma
    transfer_data(0xD0);
    transfer_data(0x05);
    transfer_data(0x09);
    transfer_data(0x09);
    transfer_data(0x08);
    transfer_data(0x03);
    transfer_data(0x24);
    transfer_data(0x32);
    transfer_data(0x32);
    transfer_data(0x3B);
    transfer_data(0x14);
    transfer_data(0x13);
    transfer_data(0x28);
    transfer_data(0x2F);

    transfer_command(0x20); 		//反显
    HAL_Delay(120);
    transfer_command(0x29);         //开启显示
}

//定义窗口坐标：开始坐标（XS,YS)以及窗口大小（x_total,y_total)
void lcd_address(uint16_t XS,uint16_t YS,uint16_t x_total,uint16_t y_total)
{
    int XE,YE;
    XE=XS+x_total-1;
    YE=YS+y_total-1;
    transfer_command(0x2a);		// 设置X开始及结束的地址
    transfer_data_16(XS);	// X开始地址(16位）
    transfer_data_16(XE);	// X结束地址(16位）

    transfer_command(0x2b);		// 设置Y开始及结束的地址
    transfer_data_16(YS);	// Y开始地址(16位）
    transfer_data_16(YE);	// Y结束地址(16位）

    transfer_command(0x2c);	    // 写数据开始
}

//指定地址显示颜色
void LCD_Fill(ulong sx,ulong sy,ulong ex,ulong ey,int color_data)
{
    ulong i,j;
    ulong width=ex-sx+1;
    ulong height=ey-sy+1;
    transfer_command(0x36);     //行扫描顺序及RGB，列扫描顺序，横放/竖放
    transfer_data(0x60);
    lcd_address(sx,sy,ex,ey);
    for(i=0;i<height;i++)
    {
        for(j=0;j<width;j++)
        {
            transfer_data_16(color_data);
        }
    }
    lcd_address(0,0,240,240);
}


void display_char16_16(unsigned int x,unsigned int y,unsigned long color,unsigned char word_serial_number)
{
    unsigned int column;
    unsigned char tm=0,temp=0,xxx=0;

    transfer_command(0x2a);    //Column address set
    transfer_data(x>>8);    //start column
    transfer_data(x);
    x=x+15;
    transfer_data(x>>8);    //end column
    transfer_data(x);

    transfer_command(0x2b);     //Row address set
    transfer_data(y>>8);    //start row
    transfer_data(y);
    y=y+15;
    transfer_data(y>>8);    //end row
    transfer_data(y);
    transfer_command(0x2C);     //Memory write


    for(column=0;column<32;column++)  //column loop
    {
        temp=chines_word[  word_serial_number ][xxx];
        for(tm=0;tm<8;tm++)
        {
            if(temp&0x01)
            {
                transfer_data(color>>8);
                transfer_data(color);
            }
            else
            {
                transfer_data(0XFF);
                transfer_data(0XFF);
            }
            temp>>=1;
        }
        xxx++;

    }
}


void display_char8_16(unsigned int x,unsigned int y,unsigned long color,unsigned char word_serial_number)
{
    unsigned int column;
    unsigned char tm=0,temp=0,xxx=0;

    transfer_command(0x2a);    //Column address set
    transfer_data(x>>8);    //start column
    transfer_data(x);
    x=x+7;
    transfer_data(x>>8);    //end column
    transfer_data(x);

    transfer_command(0x2b);     //Row address set
    transfer_data(y>>8);    //start row
    transfer_data(y);
    y=y+15;
    transfer_data(y>>8);    //end row
    transfer_data(y);
    transfer_command(0x2C);     //Memory write


    for(column=0;column<16;column++)  //column loop
    {
        temp=english_char[  word_serial_number ][xxx];
        for(tm=0;tm<8;tm++)
        {
            if(temp&0x01)
            {
                transfer_data(color>>8);
                transfer_data(color);
            }
            else
            {
                transfer_data(0XFF);
                transfer_data(0XFF);
            }
            temp>>=1;
        }
        xxx++;

    }
}

void clean_char8_16(unsigned int x,unsigned int y,unsigned long color,unsigned char word_serial_number)
{
    unsigned int column;
    unsigned char tm=0,temp=0,xxx=0;

    transfer_command(0x2a);    //Column address set
    transfer_data(x>>8);    //start column
    transfer_data(x);
    x=x+7;
    transfer_data(x>>8);    //end column
    transfer_data(x);

    transfer_command(0x2b);     //Row address set
    transfer_data(y>>8);    //start row
    transfer_data(y);
    y=y+15;
    transfer_data(y>>8);    //end row
    transfer_data(y);
    transfer_command(0x2C);     //Memory write


    for(column=0;column<16;column++)  //column loop
    {
        for(tm=0;tm<8;tm++)
        {
            transfer_data(0XFF);
            transfer_data(0XFF);
        }
        xxx++;

    }
}
void clean_char8_80(unsigned int x,unsigned int y,unsigned long color,unsigned char word_serial_number)
{
    unsigned int column;
    unsigned char tm=0,temp=0,xxx=0;

    transfer_command(0x2a);    //Column address set
    transfer_data(x>>8);    //start column
    transfer_data(x);
    x=x+7;
    transfer_data(x>>8);    //end column
    transfer_data(x);

    transfer_command(0x2b);     //Row address set
    transfer_data(y>>8);    //start row
    transfer_data(y);
    y=y+79;
    transfer_data(y>>8);    //end row
    transfer_data(y);
    transfer_command(0x2C);     //Memory write


    for(column=0;column<79;column++)  //column loop
    {
        for(tm=0;tm<8;tm++)
        {
            transfer_data(0XFF);
            transfer_data(0XFF);
        }
        xxx++;

    }
}
void display_point(unsigned int x,unsigned int y,unsigned long color)
{
    unsigned int column;
    unsigned char tm=0,temp=0xee,xxx=0;

    transfer_command(0x2a);    //Column address set
    transfer_data(x>>8);    //start column
    transfer_data(x);
    x=x+1;
    transfer_data(x>>8);    //end column
    transfer_data(x);

    transfer_command(0x2b);     //Row address set
    transfer_data(y>>8);    //start row
    transfer_data(y);
    y=y+1;
    transfer_data(y>>8);    //end row
    transfer_data(y);
    transfer_command(0x2C);     //Memory write


    for(column=0;column<2;column++)  //column loop
    {
                transfer_data(color>>8);
                transfer_data(color);
    }
}

void display_line(unsigned int x,unsigned int y,unsigned long color)
{
    unsigned int column;
    unsigned char tm=0,temp=0xee,xxx=0;

    transfer_command(0x2a);    //Column address set
    transfer_data(x>>8);    //start column
    transfer_data(x);
    x=x+2;
    transfer_data(x>>8);    //end column
    transfer_data(x);

    transfer_command(0x2b);     //Row address set
    transfer_data(y>>8);    //start row
    transfer_data(y);
    y=y+15;
    transfer_data(y>>8);    //end row
    transfer_data(y);
    transfer_command(0x2C);     //Memory write


    for(column=0;column<3*16;column++)  //column loop
    {
        transfer_data(color>>8);
        transfer_data(color);
    }
}
//void display_chars16_16(unsigned int x1,unsigned int y1,unsigned long color,unsigned char ** word_serial_number)  不可用
//{
//    uint8_t char_idx = 0;
//    unsigned int x = x1;
//    unsigned int y = y1;
//    for(;char_idx < 4 ;char_idx++ ) {
//        x=x1+char_idx*20;
//        y=y1;
//        unsigned int column;
//        unsigned char tm = 0, temp = 0, xxx = 0;
//        transfer_command(0x2a);    //Column address set
//        transfer_data(x >> 8);    //start column
//        transfer_data(x);
//        x = x + 15;
//        transfer_data(x >> 8);    //end column
//        transfer_data(x);
//
//        transfer_command(0x2b);     //Row address set
//        transfer_data(y >> 8);    //start row
//        transfer_data(y);
//        y = y + 15;
//        transfer_data(y >> 8);    //end row
//        transfer_data(y);
//        transfer_command(0x2C);     //Memory write
//
//
//        for (column = 0; column < 32; column++)  //column loop
//        {
//            temp = word_serial_number[char_idx][xxx];
//            for (tm = 0; tm < 8; tm++) {
//                if (temp & 0x01) {
//                    transfer_data(color >> 8);
//                    transfer_data(color);
//                } else {
//                    transfer_data(0XFF);
//                    transfer_data(0XFF);
//                }
//                temp >>= 1;
//            }
//            xxx++;
//
//        }
//    }
//}


void TFT_clear(void)
{
    unsigned int ROW,column;
    transfer_command(0x2a);     //Column address set
    transfer_data(0x00);    //start column
    transfer_data(0x00);
    transfer_data(0x00);    //end column
    transfer_data(0xF0);

    transfer_command(0x2b);     //Row address set
    transfer_data(0x00);    //start row
    transfer_data(0x00);
    transfer_data(0x01);    //end row
    transfer_data(0x40);
    transfer_command(0x2C);     //Memory write
    set_data_command(1);
    for(ROW=0;ROW<TFT_LINE_NUMBER;ROW++)             //ROW loop
    {

        for(column=0;column<TFT_COLUMN_NUMBER;column++)  //column loop
        {
            transfer_data_fast(0xFF);
            transfer_data_fast(0xFF);
        }
    }
}
